package edu.gxnu.cetcdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.gxnu.cetcdemo.mapper.ReserveMapper;
import edu.gxnu.cetcdemo.mapper.RoomMapper;
import edu.gxnu.cetcdemo.pojo.Reserve;
import edu.gxnu.cetcdemo.service.ReserveService;
import edu.gxnu.cetcdemo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
* @author Uplifted
* @description 针对表【reserve】的数据库操作Service实现
* @createDate 2024-04-02 15:09:11
*/
@Service
public class ReserveServiceImpl extends ServiceImpl<ReserveMapper, Reserve>
    implements ReserveService{
    @Autowired
    private ReserveMapper reserveMapper;
    @Autowired
    private RoomMapper roomMapper;
    /*
        预约状态
        WAIT_CANCEL,WAIT_AGREE,CANCEL,AGREE,DISAGREE,END
    */

    @Override
    public Result post(Reserve reserve) {
        System.out.println("reserve_roomID" + reserve.getRoomId());
        reserve.setReserveState("WAIT_AGREE");
        try {
            if (this.save(reserve))
                return new Result().build(200,"已发出预约申请，等待审核！", null);
            else
                return new Result().build(500,"申请错误！", null);
        } catch (Exception e){
            if (e.getCause().getMessage().startsWith("Duplicate")) {
                return new Result().build(400, "该条记录已存在！", null);
            }

            if (e.getCause().getMessage().startsWith("Cannot add or update a child row: a foreign key constraint fails")) {
                return new Result().build(400, "用户或机房不存在！", null);
            }
            return new Result<>().build(400,e.getMessage(),null);
        }
    }

    @Override
    public Result get() {
        List<Reserve> list = reserveMapper.selectList(null);
        return new Result<>().build(200,"查询成功！",list);
    }

    @Override
    public Result agree(Map<String, Object> map){
        LambdaUpdateWrapper<Reserve> reserveLUQ = new LambdaUpdateWrapper<>();
        Integer reserveId = (Integer) map.get("reserveId");
        Boolean agree = (Boolean) map.get("agree");
        String state = "";//用于更新状态

        String reserveState = getState(reserveId);

        //审核预约申请
        if (reserveState.equals("WAIT_AGREE")){
            state = agree ? "AGREE" : "DISAGREE";
        }
        //审核取消预约申请
        if (reserveState.equals("WAIT_CANCEL")){
            state = agree ? "CANCEL" : "AGREE_R";
        }
        reserveLUQ.eq(Reserve::getReserveId, reserveId).set(Reserve::getReserveState, state);
        try {
            reserveMapper.update(null,reserveLUQ);
            return new Result().build(200,"审核成功",null);
        } catch (Exception e) {
            System.out.println(e.getCause());
            return new Result<>().build(400, "审核失败", null);
        }

        //return new Result<>().build(400,"无需审核",null);
    }

    @Override
    public Result userOwn(Integer id) {
        LambdaQueryWrapper<Reserve> reserveLQW = new LambdaQueryWrapper<>();
        reserveLQW.eq(Reserve::getUserId, id);
        List<Reserve> list = reserveMapper.selectList(reserveLQW);

        return new Result().build(200,"查询成功！", list);
    }

    @Override
    public Result cancel(Integer id) {
        LambdaUpdateWrapper<Reserve> reserveLUW = new LambdaUpdateWrapper<>();
        String reserveState = getState(id);
        System.out.println("get reserve:"+ reserveState);
        //未批准预约直接取消
        if (reserveState.equals("WAIT_AGREE")){
            reserveMapper.deleteById(id);
            return new Result<>().build(200,"已取消预约",null);
        }

        reserveLUW.eq(Reserve::getReserveId, id).set(Reserve::getReserveState,"WAIT_CANCEL");
        try{
            reserveMapper.update(null,reserveLUW);
            return new Result<>().build(200,"取消申请已发出等待审核",null);
        } catch (Exception e){
            System.out.println(e.getCause());
            return new Result<>().build(400,"取消失败",null);
        }
    }

    private String getState(Integer Id){
        LambdaQueryWrapper<Reserve> reserveLWQ = new LambdaQueryWrapper<>();
        reserveLWQ
                .select(Reserve::getReserveState)
                .eq(Reserve::getReserveId, Id);
        Reserve stateGetter = reserveMapper.selectOne(reserveLWQ);
        return stateGetter.getReserveState();
    }
}




